package com.sundablog.service.impl.backend.system.upms.user;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.jna.platform.win32.WinUser.KBDLLHOOKSTRUCT;
import com.sundablog.mapper.AdminUserMapper;
import com.sundablog.mapper.AdminUserRoleMapper;
import com.sundablog.pojo.AdminUser;
import com.sundablog.pojo.AdminUserExample;
import com.sundablog.pojo.AdminUserExample.Criteria;
import com.sundablog.pojo.AdminUserRole;
import com.sundablog.pojo.AdminUserRoleExample;
import com.sundablog.result.LayUiResult;
import com.sundablog.service.backend.system.upms.filesystem.FilesyStemService;
import com.sundablog.service.backend.system.upms.user.UserService;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;

/**
 * 管理员接口实现类
 * @ClassName:  UserServiceImpl   
 * @Description:TODO(这里用一句话描述这个类的作用)   
 * @author: 哒哒 
 * @date:   2018年3月14日 下午7:02:19   
 *     
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private AdminUserMapper adminUserMapper;
	
	@Autowired
	private FilesyStemService filesyStemService;
	
	@Autowired
	private AdminUserRoleMapper adminUserRoleMapper;
	/**
	 * 添加管理员
	 * Title: addAdminUser   
	 * Description:    
	 * @param adminUser
	 * @return   -2(有相同的用户名)
	 * @see com.sundablog.service.backend.system.upms.user.UserService#addAdminUser(com.sundablog.pojo.AdminUser)
	 */
	@Override
	public int addAdminUser(AdminUser adminUser,int roleId) {
		//查询用户名是否有一样的
		AdminUserExample example = new AdminUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andUsernameEqualTo(adminUser.getUsername());
		List<AdminUser> list = adminUserMapper.selectByExample(example);
		if (list.size() > 0) {
			return -2;
		} 
		//添加管理员用户
		String salt = RandomUtil.simpleUUID();
		String md5PWD = DigestUtil.md5Hex(adminUser.getPassword() + salt);
		adminUser.setSalt(salt);
		adminUser.setPassword(md5PWD);
		adminUser.setCtime(new Date());
		adminUser.setLocked((byte) 0);
		int i = adminUserMapper.insertSelective(adminUser);
		if (i > 0) {
			//根据用户名查询用户信息
			AdminUserExample example1 = new AdminUserExample();
			Criteria criteria1 = example1.createCriteria();
			criteria1.andUsernameEqualTo(adminUser.getUsername());
			List<AdminUser> list2 = adminUserMapper.selectByExample(example1);
			AdminUser user = list2.get(0);	
			//用户角色关联
			AdminUserRole adminUserRole = new AdminUserRole();
			adminUserRole.setUserId(user.getUserId());
			adminUserRole.setRoleId(roleId);
			
			int j = adminUserRoleMapper.insert(adminUserRole);
			return j;
		} else {
			return i;
		}
		
		
	}

	/**
	 * 更新用户信息
	 * Title: updataAdminUser   
	 * Description:    
	 * @param adminUser
	 * @return   
	 * @see com.sundablog.service.backend.system.upms.user.UserService#updataAdminUser(com.sundablog.pojo.AdminUser)
	 */
	@Override
	public int updataAdminUser(AdminUser adminUser,int roleId) {
		AdminUser user = adminUserMapper.selectByPrimaryKey(adminUser.getUserId());
		
		if (StrUtil.isEmpty(adminUser.getPassword())) {
			adminUser.setPassword(user.getPassword());
		} else {
			String md5PWD = DigestUtil.md5Hex(adminUser.getPassword() + user.getSalt());
			adminUser.setPassword(md5PWD);
		}
		if (StrUtil.isEmpty(adminUser.getAvatar())) {
			adminUser.setAvatar(user.getAvatar());
		}
		
		
		
		int i = adminUserMapper.updateByPrimaryKeySelective(adminUser);
		if (i > 0) {
			AdminUserRoleExample example = new AdminUserRoleExample();
			com.sundablog.pojo.AdminUserRoleExample.Criteria criteria = example.createCriteria();
			criteria.andUserIdEqualTo(adminUser.getUserId());
			List<AdminUserRole> list = adminUserRoleMapper.selectByExample(example);
			AdminUserRole userRole = list.get(0);
			userRole.setRoleId(roleId);
			int j = adminUserRoleMapper.updateByPrimaryKeySelective(userRole);
			return j;
		}
		
		return i;
	}

	/**
	 * 锁定管理员用户(如果当前状态是锁定,这次调用是正常)
	 * Title: updataDisableUserId   
	 * Description:    
	 * @param userId
	 * @return   
	 * @see com.sundablog.service.backend.system.upms.user.UserService#updataDisableUserId(int)
	 */
	@Override
	public int updataDisableUserId(int userId) {
		AdminUser adminUser = adminUserMapper.selectByPrimaryKey(userId);
		if (0 == adminUser.getLocked().intValue()) {
			adminUser.setLocked((byte) 1);
		} else {
			adminUser.setLocked((byte) 0);
		}
		int i = adminUserMapper.updateByPrimaryKeySelective(adminUser);
		
		return i;
	}

	/**
	 * 根据角色ID查询用户信息
	 * Title: selectAdminUserByUserId   
	 * Description:    
	 * @param userId
	 * @return   
	 * @see com.sundablog.service.backend.system.upms.user.UserService#selectAdminUserByUserId(int)
	 */
	@Override
	public AdminUser selectAdminUserByUserId(int userId) {
		AdminUser adminUser = adminUserMapper.selectByPrimaryKey(userId);
		
		String downloadUrl = filesyStemService.getDownloadUrl(adminUser.getAvatar());
		adminUser.setAvatar(downloadUrl);
		return adminUser;
	}

	/**
	 * 分页查询
	 * Title: findPagingQuery   
	 * Description:    
	 * @param page
	 * @param limit
	 * @return   
	 * @see com.sundablog.service.backend.system.upms.user.UserService#findPagingQuery(int, int)
	 */
	@Override
	public LayUiResult findPagingQuery(int page, int limit) {
		PageHelper.startPage(page, limit);
		AdminUserExample example = new AdminUserExample();
		List<AdminUser> list = adminUserMapper.selectByExample(example);
		for (AdminUser adminUser : list) {
			String avatar = filesyStemService.getDownloadUrl(adminUser.getAvatar());
			adminUser.setAvatar(avatar);
		}
		PageInfo<AdminUser> pageInfo = new PageInfo<>(list);
		LayUiResult layUiResult = new LayUiResult();
		layUiResult.setCode(0);
		layUiResult.setMsg("");
		layUiResult.setCount(pageInfo.getTotal());
		layUiResult.setData(list);
		return layUiResult;
	}

	/**
	 * 根据角色ID 获取用户角色关联的信息
	 * Title: selectByUserId   
	 * Description:    
	 * @param userId
	 * @return   
	 * @see com.sundablog.service.backend.system.upms.user.UserService#selectByUserId(int)
	 */
	@Override
	public AdminUserRole selectByUserId(int userId) {
		AdminUserRoleExample example = new AdminUserRoleExample();
		com.sundablog.pojo.AdminUserRoleExample.Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		List<AdminUserRole> list = adminUserRoleMapper.selectByExample(example);
		return list.get(0);
	}

	/**
	 * 删除用户
	 * Title: delectUser   
	 * Description:    
	 * @param userId
	 * @return   
	 * @see com.sundablog.service.backend.system.upms.user.UserService#delectUser(int)
	 */
	@Override
	public int delectUser(int userId) {
		AdminUserRoleExample example = new AdminUserRoleExample();
		com.sundablog.pojo.AdminUserRoleExample.Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		int i = adminUserRoleMapper.deleteByExample(example);
		if (i > 0) {
			int j = adminUserMapper.deleteByPrimaryKey(userId);
			return j;
		} else {
			return 0;
		}
	}

	@Override
	public AdminUser selectAdminUserByUserName(String UserName) {
		AdminUserExample example = new AdminUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andUsernameEqualTo(UserName);
		List<AdminUser> list = adminUserMapper.selectByExample(example);
		return list.get(0);
	}

}
